package com.xuanwei.admin.controller;


import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwBannerService;
import com.xuanwei.core.util.ParamUtil;
import com.xuanwei.dao.entity.XwBanner;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mjy
 * @since 2019-06-23
 */
@RestController
@RequestMapping("api/banner")
public class BannerController {

    private IXwBannerService bannerService;

    public BannerController(IXwBannerService bannerService) {
        this.bannerService = bannerService;
    }

    @RequestMapping(value = "add.do",method = RequestMethod.POST)
    public ResultVO add(XwBanner banner){
        if(banner==null){
            return ResultVO.failResult("请传入参数");
        }
        if(StringUtils.isBlank(banner.getImageUrl())){
            return ResultVO.failResult("请上传图片");
        }
        if(StringUtils.isBlank(banner.getLinkUrl())){
            return ResultVO.failResult("请输入链接地址");
        }
        bannerService.add(banner);
        return ResultVO.successResult("保存成功");
    }

    @RequestMapping(value = "changeShow.do",method = RequestMethod.GET)
    public ResultVO changeShow(Integer id){
        if(!ParamUtil.checkIsPositiveInteger(id)){
            return ResultVO.failResult("ID格式错误");
        }
        bannerService.changeStatus(id);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "delete.do",method = RequestMethod.GET)
    public ResultVO delete(Integer id){
        if(!ParamUtil.checkIsPositiveInteger(id)){
            return ResultVO.failResult("ID格式错误");
        }
        bannerService.getBaseMapper().deleteById(id);
        return ResultVO.successResult("删除成功");
    }

    @RequestMapping(value = "list.do",method = RequestMethod.GET)
    public ResultVO list(){
        List<XwBanner> all = bannerService.findAll();
        return ResultVO.successResult(all);
    }
}
